package com.luochen.cdpt.service;

import com.luochen.cdpt.eneity.Page;
import com.luochen.cdpt.response.RespBean;
import com.luochen.cdpt.eneity.TeamInfo;
import com.luochen.cdpt.repository.ITeamInfoRepository;
import com.luochen.cdpt.util.BeanUtils;
import com.luochen.cdpt.util.SqlUtil;
import com.luochen.cdpt.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: star
 * @Date: 2018/12/16 12:34
 * @Description:
 */
@Service
public class TeamService {
    @Autowired
    ITeamInfoRepository teamInfoRepository;

    @Autowired
    EntityManager entityManager;

    public Page<Map> list(HttpServletRequest request) {
        try {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("t1.orgcde", request.getParameter("orgcde"));
            paramMap.put("t1.teamcde", request.getParameter("teamcde"));
            paramMap.put("t1.teamname", request.getParameter("teamname"));
            paramMap.put("t1.teamleader", request.getParameter("teamleader"));
            paramMap.put("t1.status", request.getParameter("status"));// 状态：status
            String sql = "select " +
                    " new Map(t1.id as id,t1.orgcde as orgcde," +
                    " t1.teamcde as teamcde,t1.teamname as teamname," +
                    "t1.teamleader as teamleader, " +
                    "o1.orgname as orgname," +
                    "t1.tcrttm as tcrttm,t1.status as status) from TeamInfo t1,OrgInfo o1 where t1.orgcde=o1.orgcde ";
            String subSql = SqlUtil.bulidSql(paramMap);
            sql = sql + subSql;
            String orgname = request.getParameter("orgname");
            if (!StringUtils.isEmptry(orgname)) {
                sql = sql + " and o1.orgname like '%" + orgname + "%'";
            }
            String tcrttm = request.getParameter("tcrttm");
            if (!StringUtils.isEmptry(tcrttm)) {
                sql = sql + " and date_format(t1.tcrttm,'%Y-%m-%d') =:tcrttm";
            }
            sql = sql +" Order By t1.tcrttm desc ";
            Query query = entityManager.createQuery(sql);
            SqlUtil.setPageParam(request, query);
            if (!StringUtils.isEmptry(tcrttm)) {
                query.setParameter("tcrttm", tcrttm);
            }
            int totalCount = query.getResultList().size();
            SqlUtil.setPageParam(request, query);
            List<Map> list = query.getResultList();
            return Page.of(totalCount, list);
        } catch (Exception e) {
            e.printStackTrace();
            return Page.error("查询失败");
        }
    }

    /**
     * 保存、更新
     *
     * @param teamInfo
     * @return
     */
    @Transactional
    public RespBean save(TeamInfo teamInfo) {
        try {
            TeamInfo teamInfo1;
            if (null != teamInfo.getId()) {
                teamInfo1 = teamInfoRepository.findById(teamInfo.getId()).get();
                BeanUtils.copyPropertiesNotNull(teamInfo1, teamInfo);
            } else {
                teamInfo1 = teamInfo;
            }
            teamInfoRepository.save(teamInfo);
            return RespBean.ok("保存成功", teamInfo1);
        } catch (Exception e) {
            return RespBean.error("保存失败");
        }
    }
}
